var Alert = {
	display: function(text, type){
    	noty({
			"text": text,
			"layout":"top", "type": type
		}); 
	}			
};


var UploadPhotoBox={
	show: function(){
		$("#box-photo-upload").modal();
	},
	progressHandling: function(){
	},
	listen: function(){
		$("form", "#box-photo-file").submit(function(e){
			e.preventDefault();
			var submit = $("button:submit", this);
			var input = $("input:file", this).get(0);


		    var formData = new FormData();
		    if(input.files.length == 0){
			    return false;
		    }
		    for(var i = 0; i < input.files.length; i ++){
				formData.append('data[Upload][]', input.files[i]);		    
		    }
			submit.hide();
			Loading.show();
		    $.ajax({
		    	url: $(this).attr("action"),
		        type: 'POST',
		        xhr: function() { 
		            var myXhr = $.ajaxSettings.xhr();
		            if(myXhr.upload){
		                myXhr.upload.addEventListener('progress', UploadPhotoBox.progressHandling, false);
		            }
		            return myXhr;
		        },
		        beforeSend: function(){
		        },
		        success: function(data){
		        	$("#upload-photo-select").prepend(data);
		        },
		        error: function(){
		        },
		        complete: function(){
		        	Loading.hide();
		        	submit.show();
		        },
		        data: formData,
		        cache: false,
		        contentType: false,
		        processData: false
		    });		    
		});		
	}
};

var Loading = {
	show: function(){
		$("#upload-photo-loading").show();
	},
	hide: function(){
		$("#upload-photo-loading").hide();
	}
};

var FacebookPhotoBox ={
	show: function(){
		$("#box-fb-import").modal();
	},		
	display: function(){
		var url = $("#fb-album-select").attr("alt");
		$("#fb-album-select").load(url);
	},	
		
	listen: function(){
		$("form", "#box-fb-album").submit(function(e){
			e.preventDefault();
	
			var submit = $("button:submit", this);
			submit.hide();
			Loading.show();
			$.ajax({
				type: "GET",
				url: $(this).attr("action"),
				data: $(this).serialize(),
				success: function (data) {	
					$("#fb-photo-select").html(data);		
				},
				error: function (data){
				},
				complete: function () {
					submit.show();
					Loading.hide();
				}
	  		});			
		});			
	},
	page: function(page){
		$("#fb-photo-select").find("li.batch").hide();
		$("#fb-photo-select").find("li.batch-" + page).show();
	},
	select: function(e){
		var selected = $(e).parent().clone();
		var a = selected.children("a");
		a.attr("onclick", "javascript:FacebookPhotoBox.deselect(this)");
		a.children("i").attr("class", "icon icon-red icon-cross over");
		$("#fb-photo-selected").append(selected);
	},

	deselect: function(e){
		$(e).parent().fadeOut(400, function() {
			$(e).parent().remove();
		});	
	}
};

var AddBox = {
	listen: function(){
		$("form", "#box-add").submit(function(e){
			e.preventDefault();
	
			var submit = $("button:submit", this);
			submit.hide();
			Loading.show();
			$.ajax({
				type: "POST",
				url: $(this).attr("action"),
				data: $(this).serialize(),
				dataType: "json",
				success: function (data) {
					if(data.error == "0"){
						Alert.display(data.data, "success");
				  		$("#reset").click();
				  		$("#fb-photo-selected").empty();
				  		$("#ItemDescription").val("");
						IndexBox.display();
					}
					else
						Alert.display(data.data, "error");					
				},
				error: function (data){
					Alert.display(data.data, "error");
				},
				complete: function () {
					Loading.hide();
					submit.show();	
				}
	  		});			
		});
	}
};

var IndexBox = {
	toggle: function(checkbox){
		$.ajax({
			url: checkbox.attr("alt"),
			dataType: "json",
			success: function (data) {
				if(data.error == "0"){
					Alert.display(data.data, "success");
					IndexBox.display();
				}
				else
					Alert.display(data.data, "error");		            
			},
			error: function (data){
				Alert.display(data.data, "error");
			},
			complete: function () {
			}
		});
	},	
	display: function(){
		var url = $("#box-index").attr("alt");
		$("#box-index").load(url, function(){
			IndexBox.listen();
		});
	},	
	listen: function(){		
		// listen ajax call
		$("a", "#box-index .ajax").click(function (e){
	    	e.preventDefault();
			var url = $(this).attr("href");
			$("#box-index").attr("alt", url);
			IndexBox.display();
		});
		
		// listen api call
		$("a.api", "#box-index").click(function (e){
		    e.preventDefault();
			if (!confirm('Are you sure you wish to delete?'))
			    return false;
			$.ajax({
				url: $(this).attr("href"),
				dataType: "json",
				success: function (data) {
					if(data.error == "0"){
						Alert.display(data.data, "success");
						IndexBox.display();
					}
					else
						Alert.display(data.data, "error");		            
				},
				error: function (data){
					Alert.display(data.data, "error");
				},
				complete: function () {
				}
			});
		});
		//toggle switch
		$("input:checkbox", "#box-index").click(function(e){
		    e.preventDefault();
		    var checkbox = $(this);
		    var status = checkbox.attr("checked");
		    checkbox.attr("disabled", true);
			$.ajax({
				url: $(this).attr("alt"),
				dataType: "json",
				success: function (data) {
					if(data.error == "0"){
						if(status == "checked" || status == true)
							checkbox.attr("checked", true);
						else
							checkbox.removeAttr("checked");
					}
				},
				complete: function () {
					checkbox.removeAttr("disabled");
				}
			});		    
		});
	}
}
